import unittest

import os
import sys
import logging
import warnings
import threading
import time

path = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
sys.path.insert(0, path)

from contract_market.initialize import config  # NOQA
from contract_market.initialize.config import *  # NOQA
from contract_market.core.actor import *  # NOQA
from contract_market.core.core import *  # NOQA
from contract_market.server.engine import EventEngine  # NOQA
from contract_market.common.common import EventSystemEngine  # NOQA
from contract_market.common.constant import *  # NOQA


def producter(manager):
    for i in range(1000):
        manager.send(i)


class CallbackObj: # depthlevel

    def __init__(self, data) -> None:
        self._data = data
        self._res = None

    def invoke(self, *args, **kwds):
        print("[CallbackObj-invoke] ", args, kwds)
        print("[CallbackObj-invoke] res ", self._res)

    def __call__(self, *args: Any, **kwds: Any) -> Any:
        self.invoke(*args, **kwds)

    def data(self):
        return self._data

    data = property(data)

    def set_res(self, res):
        self._res = res


def init_engine():
    event = EventCenter()
    engine = EventEngine(event)
    engine.add_engine(EventSystemEngine)
    print(event.obtain_events())
    return event, engine


class TestCore(unittest.TestCase):

    @classmethod
    def setUpClass(self):
        warnings.simplefilter('ignore', ResourceWarning)

    @unittest.skip
    def test_init(self):

        ma = MultiActors()
        ma.start()

        for i in range(1000):
            ma.send(i)
            # print(i)
        ma.stop()

    @unittest.skip
    def test_event_center(self):
        """ """
        event = EventCenter()
        event.start()
        producter(event)

    @unittest.skip
    def test_mutl_works(self):

        ma = MultiActors()
        ma.start()

        task = []

        for i in range(2000):
            task.append(threading.Thread(target=ma.send, args=(i,)))

        for i in task:
            i.start()

        for t in task:
            t.join()

    @unittest.skip
    def test_work(self):

        actor = Actors()
        actor.start()

        for i in range(100000):
            actor.send(i)
            # print(i)
        actor.stop()

    @unittest.skip
    def test_engine(self):

        event = EventCenter()
        engine = EventEngine(event)
        engine.add_engine(EventSystemEngine)
        print(event.obtain_events())
        # ---------------------------
        # 发起事件

        # 测试数据
        event1 = Event(XT_CANCEL_ORDER_EVENT, 13221321)
        event2 = Event(XT_SEND_ORDER_EVENT, 6666666666)
        engine.send(event1)
        engine.send(event2)

        engine.stop()
        time.sleep(1)

    # @unittest.skip
    def test_engine_callback(self):

        event, engine = init_engine()

        event1 = Event(XT_CANCEL_ORDER_EVENT, CallbackObj(1111))
        event2 = Event(XT_CANCEL_ORDER_EVENT, CallbackObj(6666))

        engine.send(event1)
        engine.send(event2)
        time.sleep(1)


if __name__ == "__main__":

    unittest.main()
    pass
